package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: xrl
 * @Date: 2022/10/13  17:59
 * @Version 1.0
 */
@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    /**
     * 查询所有频道
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 删除频道
     * 禁用的频道才能删除
     * 0：禁用   false     1：启用   true
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmChannel channel = getById(id);
        //代表状态为启用
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }
        //状态为禁用
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 频道列表查询
     */
    @Override
    public ResponseResult selectAllChannel(ChannelDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        //构建条件
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(WmChannel::getCreatedTime);
        if (!dto.getName().equals("")) {
            wrapper.eq(WmChannel::getName, dto.getName());
        }

        //调用方法查询
        page = page(page, wrapper);

        //返回结果
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        //为result 设置分页查询数据
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 新增频道
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (("").equals(wmChannel.getName().trim()) || ("").equals(wmChannel.getDescription().trim())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //非空判断
        if (wmChannel.getName() != null) {
            LambdaQueryWrapper<WmChannel> wrapper = Wrappers.<WmChannel>lambdaQuery()
                    .eq(WmChannel::getName, wmChannel.getName());
            WmChannel channel = getOne(wrapper);
            if (channel != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
            }
            //设置时间
            wmChannel.setCreatedTime(new Date());
            //保存
            save(wmChannel);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 修改频道
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //代表想禁用
        if (wmChannel.getStatus() == false) {
            LambdaQueryWrapper<WmNews> wrapper = Wrappers.<WmNews>lambdaQuery()
                    .eq(WmNews::getChannelId, wmChannel.getId());
            List<WmNews> list = wmNewsService.list(wrapper);
            if (list != null && list.size() > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED, "频道被引用,不能禁用");
            }
        }

        if (wmChannel.getId() != null) {
            WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery()
                    .eq(WmChannel::getId, wmChannel.getId()));
            channel.setName(wmChannel.getName());
            channel.setDescription(wmChannel.getDescription());
            channel.setStatus(wmChannel.getStatus());
            channel.setOrd(wmChannel.getOrd());
            //修改
            updateById(channel);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return null;
    }
}
